import headImg from './template/headImg.vue';
import comfirmLogin from '@/components/comfirm.vue';
import mintPicker from '@/pages/releaseHouse/template/mintPicker.vue';
import changeName from './memberSetFile/changeName.vue';
import changeMobile from './memberSetFile/changePhone.vue';

import { MessageBox } from 'mint-ui';
import {reLoginWX} from '@/util/util.js';
import {mapState,mapMutations} from 'Vuex';

export default {
  name: "memberSet",
  data: () => ({
    userInfo:'',
    name:'',      //昵称
    isName:false,
    comfirmTitle:{
      comfirmTop:'提示',
      comfirmCon:'是否更换绑定的手机号',
      comfirmBtn:['取消','确认'],
    },
    isWrong:false,   //显示确认框
    slots:[],
    valueKey:'',
    isMintPicker:false,
    mintPickerType:'',
    sexValue:'',
    isHeaderimg:false,
    headerPic:require('@/assets/images/userHead@2x.png'),         //用户头像
    pickerVisible:'',
    startDate:new Date('1980-1-1'),
    value: '',          //生日日期
    pickerBirthday:false,
    year:'',
    month:'',
    day:'',
    birthDay:'',
    isMobile:false,     //手机号
    mobile:'',
  }),
  components:{
    headImg,
    comfirmLogin,
    mintPicker,
    changeName,
    changeMobile
  },
  methods:{
    ...mapMutations([
      'setServicePhone',
    ]),
    chooseSex() {   //选择性别
      let that = this;
      that.isMintPicker = true;
      that.mintPickerType = '性别';
      that.slots = [{
        flex: 1,
        values:[{
          text:'男',
          sex:1,
        },{
          text:'女',
          sex:0,
        }],
        className: 'slot1',
        textAlign: 'center'
      }];
      that.valueKey = 'text';
    },
    chooseHeadimg() {
      // 选择头像
      let that = this;
      that.isHeaderimg = true;
    },
    pickerHeaderCK(data) {
      this.isHeaderimg = false;
    },
    userImg(data) {
      console.log(data);
      let that = this;
      that.headerPic = data;
      that.isHeaderimg = false;
      let iconParam = {
        icon:data
      }
      network.post(configUrl.EDITUSER_URL,iconParam,function(res){
        console.log(res);
        if(res.code==401) {
          reLoginWX();
        }else{
          window.location.reload();
        }
      },function(err){
        console.log(err);
      });
    },
    // 选择生日日期
    birthDayCK() {
      this.$refs.picker.open();
    },
    birthdayConfirm(value) {
      let valueYear = value.getFullYear(); //年
      let valueMonth = value.getMonth()+1;   //月
      let valueDay = value.getDate();      //日
      if (valueMonth<10) {
        valueMonth = '0'+valueMonth;
      }
      if (valueDay<10) {
        valueDay = '0'+valueDay;
      }
      this.year = value.getFullYear(); //年
      this.month = value.getMonth();   //月
      this.day = value.getDate();      //日
      this.birthDay = valueYear+'-'+valueMonth+'-'+valueDay;
      let birthDayParam = {
        birthday:this.birthDay
      }
      network.post(configUrl.EDITUSER_URL,birthDayParam,function(res){
        console.log(res);
        if(res.code==401) {
          reLoginWX();
        }else{
          window.location.reload();
        }
      },function(err){
        console.log(err);
      });
    },
    // 修改手机号
    cMobile() {
      this.isWrong = true;
    },
    comfirmEvent (data) {
      console.log(data);
      var that = this;
      that.isWrong = data.isWrong;
      if(data.isTrue){
        //点击了确定
        that.$router.push({path:'/changePhone',query:{}});
      }else{
        //点击了取消
      }
    },

    // 弹出框选择事件
    mintPickerBtn(data) {
      console.log(data);
      let that = this;
      let mintPickerType = that.mintPickerType;
      that.isMintPicker = false;
      switch (mintPickerType) {
        case '性别':
          that.sexValue = data.mintChooseValue.sex;
          let sexParam = {
            sex:data.mintChooseValue.sex
          }
          network.post(configUrl.EDITUSER_URL,sexParam,function(res){
            console.log(res);
            if(res.code==401) {
              reLoginWX();
            }else{
              window.location.reload();
            }
          },function(err){
            console.log(err);
          });
          break;
        default:

      }
    }
  },
  mounted() {
    let that = this;
    // var myDate = new Date(year,month,para);
    // that.startDate = myDate(1880,0,1);
    let token = window.localStorage.getItem('userToken');
    if(token=='null' || token==undefined){
      console.log('您未登录');
      MessageBox.alert('您还未登录，请先登录?').then(action => {
        that.$router.push({ name:'login'});
      });
    }
    that.$nextTick(()=>{
      console.log(that.relogin);
      if (that.relogin) {
        window.location.reload();
      }else {
        // 获取用户信息
        network.get(configUrl.QUERYUSER_URL,'',function(res){
          console.log(res);
          if(res.code==401){
            MessageBox.alert(res.message).then(action => {
                // that.$router.push({name:'login'});
                reLoginWX();
              }).catch(err=>{
                console.log(err);
              });
          }else {
            that.isLogin = true;
            that.userInfo = res.data;
            that.birthDay = res.data.birthday;
          }

        },function(err){
          console.log(err);
        });
      }

    })
    // 客服电话
    network.get(configUrl.SERVERPHONE_URL,'',function (res) {
      // console.log(res);
      var servicePhone = res.data.servicePhone;
      that.setServicePhone(servicePhone);
    },function (error) {
      console.log(error);
    });
  }
}
